<?php
namespace app\admin\controller;
use think\Db;
/**
 * 算力
 * Class NoticeController
 * Author mww
 * @package app\admin\controller
 */
class SuanliController extends CommonController
{
    /**
     * 获得数据
     */
    public function getDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        //======
        $min = '2000-02-02';
        $max = '2050-12-12';
        $where = array();
        //======
        $_POST['con_userid'] = session('loginSession')['user_id'];
        //=======
        if(!empty($_POST['start'])){
            $min = $_POST['start'];
            unset($_POST['start']);
        }
        if(!empty($_POST['end'])){
            $max = $_POST['end'];
            unset($_POST['end']);
        }
        if(!empty($_POST['com_status']) && $_POST['com_status'] == 4){
            $_POST['con_status'] = 2;
            unset($_POST['com_status']);
        }
        $where['con_insertdate'] = array('between',"$min,$max");
        //=======
        $suanli = Db::table('ew_contract_com')->alias('cc')
            ->join('ew_computer c','cc.cc_computerid=c.com_id','LEFT')
            ->join('ew_contract con','cc.cc_contractid=con.con_id','LEFT')
            ->where($_POST)->where($where)->order('com_id DESC')
            ->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_contract_com')->alias('cc')
            ->join('ew_computer c','cc.cc_computerid=c.com_id','LEFT')
            ->join('ew_contract con','cc.cc_contractid=con.con_id','LEFT')
            ->where($_POST)->count();
        //=======这里显示份额下相对应的金额=======
        if(!empty($suanli)){
            foreach ($suanli as $k=>$v){
                if($v['con_money'] > 0){
                    $res = ($v['cc_many']/$v['con_allmany'])*$v['con_money'];
                    $suanli[$k]['con_money'] = substr(sprintf("%.3f", $res),0,-1);
                }
                }
        }

        //===================================
        $data['total'] = $total;
        $data['rows'] = $suanli;
        return json_encode($data);
    }
    /**
     * 修改收益方式
     */
    public function changeDisplay()
    {
        $display_type = $_POST['display_type'];
        $id = session('loginSession')['user_id'];
        if(empty($display_type)){
            return json_encode(['status'=>0,'msg'=>'参数错误，请刷新重试']);
        }
        if($display_type == 1){
            $value = 2;
        }else{
            $value = 1;
        }
        $res = Db::table('ew_user')->where('user_id',$id)->update(['user_display'=>$value]);
        //==============记录===========
        $data = array();
        $data['remind_whotowho'] = $display_type;
        $data['remind_userid'] = $id;
        $data['remind_time'] = date('Y-m-d H:i:s');
        $data['remind_type'] = 5;
        $data['remind_ip'] = $_SERVER["REMOTE_ADDR"];
        //==============end============
        if($res){
            $data['remind_status'] = 1;
            Db::table('ew_remind')->insert($data);//====
            return json_encode(['status'=>200,'msg'=>'更改成功']);
        }else{
            $data['remind_status'] = 2;
            Db::table('ew_remind')->insert($data);//====
            return json_encode(['status'=>0,'msg'=>'更改失败']);
        }
    }

    //==========================这是管理员操作分割线==================================
    /**
     * 获得数据
     */
    public function admingetDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $suanli = Db::table('ew_computer')
            ->where($_POST)->order('com_id DESC')
            ->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_computer')
            ->where($_POST)->count();
        $data['total'] = $total;
        $data['rows'] = $suanli;
        return json_encode($data);
    }
    /**
     * 显示添加页
     */
    public function add()
    {
        return $this->fetch("suanli/add");
    }
    /**
     * 添加机器
     */
    public function insert()
    {
        foreach ($_POST as $key => $value) {
            $value = trim($value,' ');
            if($value == ""){
                return json_encode(['status'=>0,'msg'=>'有未填项']);
            }
        }
        $num = Db::table('ew_computer')->where('com_number',$_POST['num'])->find();
        if(!empty($num)){
            return json_encode(array("status" => 0, "msg" => "机器编号重复，请重新输入！"));
        }
        $data = array();
        $data['com_number'] = trim($_POST['num'],' ');
        $data['com_count'] = trim($_POST['count'],' ');
        $data['com_power'] = trim($_POST['power'],' ');
        $data['com_status'] = trim($_POST['status'],' ');
        $data['com_all'] = 10;
        $data['com_use'] = 0;
        $data['com_have'] = 10;
        $data['com_date'] = date('Y-m-d');
        $res = Db::table('ew_computer')->insert($data);
        if ($res) {
            return json_encode(array("status" => 200, "msg" => "新增成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "新增失败！"));
        }
    }
    /**
     * 显示修改页
     */
    public function updatemodel()
    {
        $id = input('com_id', '');
        $data = Db::table('ew_computer')->where('com_id',$id)->find();
        $this->assign("data", $data);
        return $this->fetch("suanli/update");
    }
    /**
     * 修改
     */
    public function update()
    {
        foreach ($_POST as $key => $value) {
            $value = trim($value,' ');
            if($value == ""){
                return json_encode(['status'=>0,'msg'=>'有未填项']);
            }
        }
        $id = $_POST['com_id'];
        $num = Db::table('ew_computer')->where('com_id',$id)->find();
        $number = trim($_POST['num'],' ');
        if($num['com_number'] != $number){
            $count = Db::table('ew_computer')->where('com_number',$number)->count();
            if($count >= 1){
                return json_encode(array("status" => 0, "msg" => "机器编号重复，请重新输入！"));
            }
        }
        $data = array();
        $data['com_number'] = $number;
        $data['com_count'] = trim($_POST['count'],' ');
        $data['com_power'] = trim($_POST['power'],' ');
        $data['com_status'] = trim($_POST['status'],' ');
        $res = Db::table('ew_computer')->where('com_id',$id)->update($data);
        if ($res) {
            return json_encode(array("status" => 200, "msg" => "修改成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "修改失败或者您未做任何修改！"));
        }
    }
    /**
     * 删除
     */
    public function delete()
    {
        $id = $_POST['id'];
        $have = Db::table('ew_contract_com')->alias('cc')
            ->join('ew_contract con','cc.cc_contractid=con.con_id','LEFT')
            ->where('con_status',1)
            ->where('cc_computerid',$id)
            ->find();
        if(!empty($have)){
            return json_encode(array("status" => 0, "msg" => "此机器还有分配出去的份额！"));
        }
        $deleteinfo = Db::table('ew_computer')->where('com_id',$id)->delete();
        if ($deleteinfo) {
            return json_encode(array("status" => 200, "msg" => "删除成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "删除失败！"));
        }
    }

}